package www.openteam.com.adapter;

import android.content.Context;
import android.support.v7.util.SortedList;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import www.openteam.com.R;
import www.openteam.com.bean.CustomConditon;
import www.openteam.com.bean.CustomType;
import www.openteam.com.bean.QueryCustom;

/**
 * Created by kylin on 2016/9/26.
 */

public class CustomAdapter extends RecyclerView.Adapter {


    private static final String TAG = "CustomAdapter";
    private LayoutInflater mLayoutInflater;
    private SortedList<QueryCustom.DataBean> mSortedList;
    private ArrayList<CustomType.DataBean> customTypes = new ArrayList<CustomType.DataBean>();
    private ArrayList<CustomType.DataBean> moneyTypes = new ArrayList<CustomType.DataBean>();
    private ArrayList<CustomType.DataBean> copayTypes = new ArrayList<CustomType.DataBean>();
    private ArrayList<CustomConditon> customConditons = new ArrayList<CustomConditon>();
    private Context context;
    private CustomAdapter.ButtonInterface buttonInterface;
    private CustomConditon customConditon;

    public CustomAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
//        Log.d(TAG, "DataAdapter: 1");
        mSortedList = new SortedList<>(QueryCustom.DataBean.class, new SortedList.Callback<QueryCustom.DataBean>() {

            /**
             * 返回一个负整数（第一个参数小于第二个）、零（相等）或正整数（第一个参数大于第二个）
             */
            @Override
            public int compare(QueryCustom.DataBean o1, QueryCustom.DataBean o2) {

                if (Integer.parseInt(o1.getRownumber()) < Integer.parseInt(o2.getRownumber())) {
                    return -1;
                } else if (Integer.parseInt(o1.getRownumber()) > Integer.parseInt(o2.getRownumber())) {
                    return 1;
                }

                return 0;
            }

            @Override
            public boolean areContentsTheSame(QueryCustom.DataBean oldItem, QueryCustom.DataBean newItem) {
                Log.d(TAG, "areContentsTheSame: " + 1);
                return oldItem.getRownumber().equals(newItem.getRownumber());
            }

            @Override
            public boolean areItemsTheSame(QueryCustom.DataBean item1, QueryCustom.DataBean item2) {

                boolean statue;
                if (item1.getId() == item2.getId() &&
                        item1.getCode().equals(item2.getCode()) &&
                        item1.getName().equals(item2.getName())) {
                    statue = true;
                } else {
                    statue = false;
                }

                return statue;

            }

            @Override
            public void onInserted(int position, int count) {
                notifyItemRangeInserted(position, count);
            }

            @Override
            public void onRemoved(int position, int count) {
                notifyItemRangeRemoved(position, count);
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {
                notifyItemMoved(fromPosition, toPosition);
            }

            @Override
            public void onChanged(int position, int count) {
                notifyItemRangeChanged(position, count);
            }
        });
    }


    public void addItems(ArrayList<QueryCustom.DataBean> list) {
        mSortedList.beginBatchedUpdates();
        for (QueryCustom.DataBean itemModel : list) {
            mSortedList.add(itemModel);
        }
        mSortedList.endBatchedUpdates();

    }

    public void addItemM(ArrayList<CustomType.DataBean> customType,
                         ArrayList<CustomType.DataBean> moneyTypes,
                         ArrayList<CustomType.DataBean> copayTypes,
                         Context context) {
        for (CustomType.DataBean mt : moneyTypes
                ) {
            this.moneyTypes.add(mt);
        }

        for (CustomType.DataBean mt : copayTypes
                ) {
            this.copayTypes.add(mt);
        }
        for (CustomType.DataBean mt : customType
                ) {
            customTypes.add(mt);
        }
        this.context = context;
        Log.d(TAG, "addItemM: " + customType.toString());
    }

    public void deleteItems(ArrayList<QueryCustom.DataBean> items) {
        mSortedList.beginBatchedUpdates();
        for (QueryCustom.DataBean item : items) {
            mSortedList.remove(item);
        }
        mSortedList.endBatchedUpdates();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.d(TAG, "DataAdapter: 3");
        return new CustomAdapter.mViewHolder(mLayoutInflater.inflate(R.layout.item_custom, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//        Log.d(TAG, "DataAdapter:4");
        CustomAdapter.mViewHolder viewHolder = (CustomAdapter.mViewHolder) holder;
        QueryCustom.DataBean item = mSortedList.get(position);
        int type = 0;
        int paytype = 0;
        int moneytype = 0;

        for (int i = 0; i < customTypes.size(); i++) {
            if (customTypes.get(i).getCode().equals(item.getType())) {
                type = i;

            }

        }
        for (int i = 0; i < moneyTypes.size(); i++) {
            if (moneyTypes.get(i).getCode().equals(item.getMoneytype())) {
                moneytype = i;
            }

        }
        for (int i = 0; i < copayTypes.size(); i++) {
            if (copayTypes.get(i).getCode().equals(item.getPaytime())) {
                paytype = i;
                Log.d(TAG, "onBindViewHolder: " + i);
            }

        }
        customConditon = new CustomConditon(type, paytype, moneytype);
        viewHolder.tv_rowno.setText("\t\t\t\t" + item.getRownumber());
//        viewHolder.spinner_custom.setAdapter(new MyAdapter(customTypes, item.getType(), this.context));
//        viewHolder.spinner_custom.setSelection(type);
//        viewHolder.spinner_moneytype.setAdapter(new MyAdapter(moneyTypes, item.getType(), this.context));
//        viewHolder.spinner_moneytype.setSelection(moneytype);
        //   viewHolder.spinner_paytype.setAdapter(new MyAdapter(copayTypes, item.getType(), this.context));
//        viewHolder.spinner_paytype.setSelection(paytype);
        viewHolder.spinner_custom.setText(customTypes.get(type).getName());
        viewHolder.spinner_moneytype.setText(moneyTypes.get(moneytype).getName());
        viewHolder.spinner_paytype.setText(copayTypes.get(paytype).getName());
        viewHolder.tv_customname.setText(item.getName());
        viewHolder.tv_customno.setText(item.getCode());
        viewHolder.tv_infoamout.setText(item.getCreditNumb());
        viewHolder.tv_takegoods.setText(item.getSendAddress());
        viewHolder.tv_contact_custom.setText(item.getConnecter());
        viewHolder.phone.setText(item.getCTel());
        viewHolder.salesman.setText(item.getBusinessMan());

        customConditons.add(customConditon);
        viewHolder.btn_new.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (buttonInterface != null) {
//                  接口实例化后的而对象，调用重写后的方法
                    buttonInterface.onclick(v, position, customConditons, customTypes,
                            moneyTypes, copayTypes);

                }

            }
        });

        viewHolder.btn_modify.setOnClickListener(new View.OnClickListener() {
                                                     @Override
                                                     public void onClick(View v) {
                                                         if (buttonInterface != null) {
                                                             buttonInterface.modify(v, position,
                                                                     customConditons, customTypes,
                                                                     moneyTypes, copayTypes, mSortedList);
                                                         }
                                                     }
                                                 }
        );

        viewHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buttonInterface != null) {
                    buttonInterface.delete(mSortedList.get(position).getId());
                }
            }
        });
    }


    /**
     * 按钮点击事件需要的方法
     */
    public void buttonSetOnclick(CustomAdapter.ButtonInterface buttonInterface) {
        this.buttonInterface = buttonInterface;
    }

    /**
     * 按钮点击事件对应的接口
     */
    public interface ButtonInterface {
        void onclick(View view, int position,
                     ArrayList<CustomConditon> customConditons,
                     ArrayList<CustomType.DataBean> customTypes,
                     ArrayList<CustomType.DataBean> moneyTypes,
                     ArrayList<CustomType.DataBean> copayTypes
        );

        void modify(View view, int position,
                    ArrayList<CustomConditon> customConditons,
                    ArrayList<CustomType.DataBean> customTypes,
                    ArrayList<CustomType.DataBean> moneyTypes,
                    ArrayList<CustomType.DataBean> copayTypes,
                    SortedList<QueryCustom.DataBean> mSortedList
        );

        void delete(int id);
    }


    @Override
    public int getItemCount() {

//        Log.d(TAG, "getItemCount: " +mSortedList.size());
        return mSortedList.size();
    }

    static class mViewHolder extends RecyclerView.ViewHolder {

        TextView spinner_custom;
        TextView tv_customno;
        TextView tv_customname;
        TextView spinner_moneytype;
        TextView spinner_paytype;
        TextView salesman;
        TextView tv_infoamout;
        TextView tv_takegoods;
        TextView tv_contact_custom;
        TextView phone;
        TextView tv_rowno;
        Button btn_new, btn_modify, btn_delete;


        public mViewHolder(View itemView) {
            super(itemView);
            spinner_custom = (TextView) itemView.findViewById(R.id.spinner_custom);
            tv_customno = (TextView) itemView.findViewById(R.id.tv_customno);
            tv_customname = (TextView) itemView.findViewById(R.id.tv_customname);
            spinner_moneytype = (TextView) itemView.findViewById(R.id.spinner_moneytype);
            spinner_paytype = (TextView) itemView.findViewById(R.id.spinner_paytype);
            salesman = (TextView) itemView.findViewById(R.id.salesman);
            tv_infoamout = (TextView) itemView.findViewById(R.id.tv_infoamout);
            tv_takegoods = (TextView) itemView.findViewById(R.id.tv_takegoods);
            tv_contact_custom = (TextView) itemView.findViewById(R.id.tv_contact_custom);
            phone = (TextView) itemView.findViewById(R.id.phone);
            tv_rowno = (TextView) itemView.findViewById(R.id.tv_rowno);
            btn_new = (Button) itemView.findViewById(R.id.btn_new);
            btn_modify = (Button) itemView.findViewById(R.id.btn_modify);
            btn_delete = (Button) itemView.findViewById(R.id.btn_delete);
        }
    }


    class MyAdapter extends BaseAdapter {

        List<CustomType.DataBean> mList = null;
        Context mContext = null;
        String code = null;

        public MyAdapter(List mList, String code, Context mContext) {
            this.mList = mList;
            this.mContext = mContext;
            this.code = code;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater _LayoutInflater = LayoutInflater.from(mContext);
            convertView = _LayoutInflater.inflate(R.layout.item_user, null);
            if (convertView != null) {
                TextView tv = (TextView) convertView.findViewById(R.id.spinner_item);
                tv.setText(mList.get(position).getName());
            }
            return convertView;
        }
    }
}